#include "Dictionary.h"

void Dictionary::initDictCh(const string &dictPathCh){
    ifstream ifs(dictPathCh);
    if(!ifs){
        cerr<<"can not open file dictPathCh"<<endl;
        return;
    }
    string line;
    while(getline(ifs,line)){
        istringstream iss(line);
        string key;
        int value;
        if(iss>>key>>value){
            _dictCh.push_back(make_pair(key,value));
        }
        else{
        cerr<<"can not analyze _dictCh line:"<<key<<endl;
        }
    }
}
void Dictionary::initDictEn(const string &dictPathEn){
    ifstream ifs(dictPathEn);
    if(!ifs){
        cerr<<"can not open file dictPathEn"<<endl;
        return;
    }
    string line;
    while(getline(ifs,line)){
        istringstream iss(line);
        string key;
        int value;
        if(iss>>key>>value){
            _dictEn.push_back(make_pair(key,value));
        }
        else{
        cerr<<"can not analyze _dictEn line:"<<key<<endl;
        }
    }
}

void Dictionary::initIndexCh(const string &indexPathCh){
    ifstream ifs(indexPathCh);
    if(!ifs){
        cerr<<"can not open indexPathCh"<<endl;
        return;
    }
    string line;
    while(getline(ifs,line)){
        string key;
        istringstream iss(line);
        if(iss>>key){
            int value;
            while(iss>>value){
                _indexCh[key].insert(value);
            }
        }
    }
}
void Dictionary::initIndexEn(const string &indexPathEn){
    ifstream ifs(indexPathEn);
    if(!ifs){
        cerr<<"can not open indexPathEn"<<endl;
        return;
    }
    string line;
    while(getline(ifs,line)){
        string key;
        istringstream iss(line);
        if(iss>>key){
            int value;
            while(iss>>value){
                _indexEn[key].insert(value);
            }
        }
    }
}

vector<pair<string,int>>& Dictionary::getDictCh(){return _dictCh;}
vector<pair<string,int>>& Dictionary::getDictEn(){return _dictEn;}

map<string,set<int>>& Dictionary::getIndexCh(){return _indexCh;}
map<string,set<int>>& Dictionary::getIndexEn(){return _indexEn;}
//vector<pair<string,int>>& Dictionary::getFinRes(){return _RecWord;} 

map<string,set<int>>Dictionary::findWord(const char ch,map<string,set<int>> _index){
    std::map<std::string,std::set<int>> finIt;
    for (auto it = _index.begin(); it != _index.end(); ++it){
        for(auto ch1:it->first){
            if(ch==ch1){
                finIt.insert(make_pair(it->first,it->second));
            }
        }
    }
    return finIt;
}


vector<pair<string,int>>& Dictionary::doAndQuery(const string & str){
//void Dictionary::doAndQuery(const string & str){
    set<int> finRes;
    bool isch =false;
    for(auto ch:str){
        if(isChinese(ch)){
            isch=true;
            map<string,set<int>> finIt = findWord(ch,_indexCh);
            for(auto it:finIt){
                set<int> memSet=it.second;
                finRes.insert(memSet.begin(),memSet.end());
            }
        }else if(isEnglish(ch)){
            auto it = _indexEn.find(std::string(1,ch));
            // cout << endl;
                set<int> memSet=it->second;
                finRes.insert(memSet.begin(),memSet.end());
        }else{
            continue;
        }
    }
    if(isch==true){
        for(auto elem:finRes){
            _RecWord.push_back(make_pair(_dictCh[elem-1].first,_dictCh[elem-1].second));
        }
    }else{
        for(auto elem:finRes){
            _RecWord.push_back(make_pair(_dictEn[elem - 1].first,_dictEn[elem-1].second));
        }
    }
    return _RecWord;
}
    
bool Dictionary::isChinese(const char ch){
    if (ch >= '\xe4' && ch <= '\xe9') {
        return true;
    }
    return false;
}

bool Dictionary::isEnglish(const char ch){
    if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
        return true;
    }
    return false;
}


